package edu.guigu.appquickstart.datastore.dao.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

import edu.guigu.appquickstart.datastore.bean.UserBean;
import edu.guigu.appquickstart.datastore.dao.IUserDao;
import edu.guigu.appquickstart.datastore.dao.DBHelper;

/**
 * User表数据操作类
 *
 * @author yuanc
 * @Description: (用一句话描述)
 * @date 2025/8/12 20:18
 */
public class UserDao implements IUserDao {
    private DBHelper dbHelper;

    public UserDao(Context context) {
        dbHelper = DBHelper.getInstance(context);
    }

    @Override
    public long addUser(UserBean user) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_USERNAME, user.getName());
        values.put(COLUMN_EMAIL, user.getEmail());
        values.put(COLUMN_AGE, user.getAge());

        return dbHelper.insert(TABLE_USERS, values);
    }

    @Override
    public List<UserBean> getAllUsers() {
        List<UserBean> userList = new ArrayList<>();
        Cursor cursor = dbHelper.queryAll(TABLE_USERS);

        if (cursor != null && cursor.moveToFirst()) {
            do {
                UserBean user = new UserBean();
                user.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID)));
                user.setName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_USERNAME)));
                user.setEmail(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_EMAIL)));
                user.setAge(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_AGE)));
                user.setRegisterTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_REGISTER_TIME)));
                userList.add(user);
            } while (cursor.moveToNext());
            cursor.close();
        }

        return userList;
    }

    @Override
    public UserBean getUserById(int id) {
        Cursor cursor = dbHelper.query(TABLE_USERS, null, "id = ?",
                new String[]{String.valueOf(id)}, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            UserBean user = new UserBean();
            user.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID)));
            user.setName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_USERNAME)));
            user.setEmail(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_EMAIL)));
            user.setAge(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_AGE)));
            user.setRegisterTime(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_REGISTER_TIME)));
            cursor.close();
            return user;
        }
        return null;
    }

    @Override
    public int updateUser(UserBean user) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_USERNAME, user.getName());
        values.put(COLUMN_EMAIL, user.getEmail());
        values.put(COLUMN_AGE, user.getAge());

        return dbHelper.update(TABLE_USERS, values, "id = ?",
                new String[]{String.valueOf(user.getId())});
    }

    @Override
    public int deleteUser(int userId) {
        return dbHelper.delete(TABLE_USERS, "id = ?",
                new String[]{String.valueOf(userId)});
    }

    @Override
    public void bulkAddUsers(List<UserBean> users) {
        List<ContentValues> valuesList = new ArrayList<>();
        for (UserBean user : users) {
            ContentValues values = new ContentValues();
            values.put(COLUMN_USERNAME, user.getName());
            values.put(COLUMN_EMAIL, user.getEmail());
            values.put(COLUMN_AGE, user.getAge());
            valuesList.add(values);
        }
        dbHelper.bulkInsert(TABLE_USERS, valuesList);
    }
}
